package bTree.LeetCode;

import javax.swing.tree.TreeNode;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
public class 二叉搜索树迭代器 {
    Queue<TreeNode> queue = new LinkedList<>();

    public 二叉搜索树迭代器(TreeNode root) {
        init(root);

    }

    private void init(TreeNode root) {
        if (root==null){
            return;
        }
        init(root.left);
        queue.add(root);
        init(root.right);
    }

    public int next() {
        return queue.poll().val;
    }

    public boolean hasNext() {
        return queue.size()!=0;
    }
}

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator obj = new BSTIterator(root);
 * int param_1 = obj.next();
 * boolean param_2 = obj.hasNext();
 */